<?php

namespace addons\sum\service\agent;

use addons\sum\exception\SumException;
use app\admin\model\sum\agent\Apply as ApplyModel;
use app\admin\model\sum\data\Area as AreaModel;
use app\admin\model\sum\user\User;
use think\Db;

class Agent
{

    protected $user = null;

    public function __construct($user)
    {
        $this->user = is_numeric($user) ? User::get($user) : $user;
    }

    /**
     * 提交审核
     * @param $params
     * @return ApplyModel|array|bool|\PDOStatement|string|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function apply($params)
    {
        // 查询审核未通过的或者驳回的门店申请信息
//        $apply = ApplyModel::where('user_id', $this->user->id)->where('status', 'in', [-1, 0])->find();
        $apply = ApplyModel::where('user_id', $this->user->id)->find();
        if($apply){
            if($apply['status']==0){
                throw new SumException('申请审核中');
            }
            if($apply['status']==1){
                throw new SumException('不能重复申请');
            }
        }



        $params = $this->getAreaIdByName($params);
        $params['user_id'] = $this->user->id;
        $params['status'] = 0;
        if ($apply) {
            $params['apply_num'] = $apply->apply_num + 1;
            $apply->save($params);
        } else {
            $params['apply_num'] = 1;
            $apply = new ApplyModel();
            $apply->save($params);
        }
        Db::name('user')->where('id',$this->user->id)->update(['agent_status'=>1]);
        return $apply;
    }

    // 同意
    public function handleAgree($apply, $oper = null)
    {
        if ($apply->status != 0) {
            throw new SumException('请勿重复操作');
        }
        $apply->status = 1;
        $apply->save();
        Db::name('user')->where('id',$this->user->id)->update(['agent_status'=>2]);

        return $apply;
    }
    // 拒绝
    public function handleRefuse($apply, $refuse_msg)
    {
        if ($apply->status != 0 && $apply->status != 1) {
            throw new SumException('请勿重复操作');
        }
        $apply->status = -1;
        $apply->status_msg = $refuse_msg;
        $apply->save();
        Db::name('user')->where('id',$this->user->id)->update(['agent_status'=>-1]);

        return $apply;
    }

    private function getAreaIdByName($params)
    {
        $province = AreaModel::where([
            'name' => $params['province_name'],
            'level' => 'province'
        ])->find();
        if (!$province){
            throw new SumException('请选择正确的行政区');
        }

        $params['province_id'] = $province->id;

        $city = AreaModel::where([
            'name' => $params['city_name'],
            'level' => 'city',
            'pid' => $province->id
        ])->find();
        if (!$city){
            throw new SumException('请选择正确的行政区');
        }
        $params['city_id'] = $city->id;

        $district = AreaModel::where([
            'name' => $params['district_name'],
            'level' => 'district',
            'pid' => $city->id
        ])->find();
        if (!$district){
            throw new SumException('请选择正确的行政区');
        }
        $params['district_id'] = $district->id;

        return $params;
    }
}
